<?php
namespace app\store\model\activity;

use app\common\model\store\shop\Clerk;
use think\Model;

class Manba1 extends Model{
    const FORM_SCENE_ADD = 'add';
    const FORM_SCENE_EDIT = 'edit';

    protected $name = 'avtivity_list';

    /**
     * 获取列表数据
     * @param int $status 状态
     * @param int $shop_id 门店id
     * @param string $search 店员姓名/手机号
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($search = '',$id='')
    {
        $this->where('activity_id',$id);
        // 检索查询条件
//        $status > -1 && $this->where('status', '=', (int)$status);
//        $shop_id > 0 && $this->where('shop_id', '=', (int)$shop_id);
        !empty($search) && $this->where('phone|user_name', 'like', "%{$search}%");
        // 查询列表数据
        return $this
            ->paginate(15, false, [
                'query' => \request()->request()
            ]);
    }

    /**
     * 新增记录
     * @param $data
     * @return bool
     * @throws \Exception
     */
    public function add($data)
    {
//        这是用最垃圾的代码实现最垃圾的逻辑勿看

        // 表单验证
        if (!$this->validateForm($data, self::FORM_SCENE_ADD)) {
            return false;
        }
        foreach ($data['bei_img'] as $k=>$v){
            $data['bei_img_id'][]=$v;
            $file=db('upload_file')->where('file_id',$v)->find();
            $data['bei_img'][$k]='http://'.$_SERVER['SERVER_NAME'].'/uploads/'.$file['file_name'];
        }

        $data['bei_img']=implode(',',$data['bei_img']);
        $data['bei_img_id']=implode(',', $data['bei_img_id']);


        $goods=db('goods')->where('goods_id',$data['goods_id'])->find();
        $shop=db('store_shop')->where('shop_id',$data['shop_id'])->find();
//        $data['wxapp_id'] = self::$wxapp_id;
        $data['lat']=$shop['latitude'];
        $data['lng']=$shop['longitude'];
        $data['shop_name']=$shop['shop_name'];
        $data['shop_phone']=$shop['phone'];
        $data['goods_name']=$goods['goods_name'];

        return $this->allowField(true)->save($data);
    }

    /**
     * 编辑记录
     * @param $data
     * @return bool|false|int
     * @throws \think\exception\DbException
     */
    public function edit($data)
    {
        // 表单验证
        if (!$this->validateForm($data, self::FORM_SCENE_EDIT)) {
            return false;
        }
        foreach ($data['bei_img'] as $k=>$v){
            $data['bei_img_id'][]=$v;
            $file=db('upload_file')->where('file_id',$v)->find();
            $data['bei_img'][$k]='http://'.$_SERVER['SERVER_NAME'].'/uploads/'.$file['file_name'];
        }

        $data['bei_img']=implode(',',$data['bei_img']);
        $data['bei_img_id']=implode(',', $data['bei_img_id']);


        $goods=db('goods')->where('goods_id',$data['goods_id'])->find();
        $shop=db('store_shop')->where('shop_id',$data['shop_id'])->find();
//        $data['wxapp_id'] = self::$wxapp_id;
        $data['lat']=$shop['latitude'];
        $data['lng']=$shop['longitude'];
        $data['shop_name']=$shop['shop_name'];
        $data['shop_phone']=$shop['phone'];
        $data['goods_name']=$goods['goods_name'];
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 表单验证
     * @param $data
     * @param string $scene
     * @return bool
     * @throws \think\exception\DbException
     */
    private function validateForm($data, $scene = self::FORM_SCENE_ADD)
    {
//        if ($scene === self::FORM_SCENE_ADD) {
        if (!isset($data['bei_img']) || empty($data['bei_img'])) {
            $this->error = '活动图必选';
            return false;
        }
        if (!isset($data['goods_id']) || empty($data['goods_id'])) {
            $this->error = '产品必选';
            return false;
        }
//            if (self::detail(['user_id' => $data['user_id'], 'is_delete' => 0])) {
//                $this->error = '该用户已经是店员，无需重复添加';
//                return false;
//            }
//        }
        return true;
    }

    /**
     * 店员详情
     * @param $where
     * @return static|null
     * @throws \think\exception\DbException
     */
    public static function detail($where)
    {
        $filter = is_array($where) ? $where : ['id' => $where];
        return static::get($filter);
    }


}